Function.RegisterNamespace("System.IO.DirectoryStrategy.V8");

System.IO.DirectoryStrategy.V8.Windows=function(){
    // IDirectoryStrategy Members
    this.Exists=function(path){
        if(!path||System.Environment.Execute("cmd", ["/C",String.Format("IF NOT EXIST \"{0}\" (ECHO 1)",escapePath(path))])==1)return false;
        return System.Environment.Execute("cmd", ["/C",String.Format("2>NUL PUSHD \"{0}\" && (POPD&ECHO 1) || (ECHO 0)",escapePath(path))])==1;
    };

    this.GetFiles=function(path){
        return get(path, false);
    };
    
    this.GetDirectories=function(path){
        return get(path, true);
    };

    // IStrategySpecification Members
    this.IsSatisfiedBy=function(candidate){
        if(typeof(os)!='undefined' && typeof(os.system)!='undefined'){
            try{
                os.system("ver");
                System.IO.Path.DirectorySeparator="\\";
                return true;
            }catch(e){}
        }
        return false;
    };

    // Private Methods
    function escapePath(path){
        return (path||'').replace(/\&/g,"^&");
    }

    function get(path,directories){
        path=System.IO.Path.GetFullPath(path);
        var items=String.Trim(System.Environment.Execute("cmd", ["/C",String.Format("for {1}%a in (\"{0}\") do @echo %a",System.IO.Path.Combine(path,"*"),directories?"/d ":'')])).split("\n");
        for(var i=0;i<items.length;i++){
            items[i]=String.Trim(items[i]);
            if(!items[i])items.splice(i--,1);
        }
        return items;
    }
};

System.IO.DirectoryStrategy.V8.Windows.Implement(System.IO.DirectoryStrategy.IDirectoryStrategy,'System.IO.DirectoryStrategy.V8.Windows');
System.IO.DirectoryStrategy.V8.Windows.Implement(System.Script.Strategy.IStrategySpecification,'System.IO.DirectoryStrategy.V8.Windows');

System.IO.Directory.Strategies.Add(System.IO.DirectoryStrategy.V8.Windows);